<?php
/**
 * 地址模块
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\UserService;

use App\Services\UserService\UserTool;
use DB;
use Illuminate\Http\Request;

class AddressService
{
    protected $UserTool;

    public function __construct(UserTool $UserTool)
    {
        $this->UserTool = $UserTool;
    }

    /**
     * 添加收件地址
     */
    public function addAddress(Request $request)
    {
        $user_id = $request->get('user_id');
        if (strlen($user_id) == 0) {
            $result = array('message' => '请先登录', 'result' => (Object) array(), 'state' => 'M09000');
            return $result;
        }
        $consignee = $request->get('consignee');
        if (mb_strlen($consignee) > 12) {
            $arr = array('message' => '收货人姓名太长!!!', 'result' => (object) array(), 'state' => 'M09009');
            return $arr;
        }
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $consignee) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i", json_encode($consignee)) != 0) {
            //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '收货人姓名中不能含有特殊字符', 'result' => (object) array(), 'state' => 'M09009');
            return $arr;
        }
        $phone = $request->get('phone');
        $country = $request->get('country', '');
        $city = $request->get('city');
        $address = $request->get('address');
        if (preg_match("/(\\\u[ed][0-9a-f]{3})/i", json_encode($address)) != 0) {
            //不允许特殊字符 判断emoji表情
            $arr = array('message' => '详细地址中不能含有表情', 'result' => (object) array(), 'state' => 'M09009');
            return $arr;
        }
        $isDefault = $request->get('is_default');
        $time = time();
        if (strlen($consignee) == 0 || strlen($phone) == 0 || strlen($city) == 0 || strlen($address) == 0) {
            $result = array('message' => '请检查信息完整', 'result' => (Object) array(), 'state' => 'M00002');
            return $result;
        }
        $check = $this->UserTool->checkPhone($phone);
        if ($check == false) {
            $result = array('message' => '手机号码格式不正确', 'result' => (Object) array(), 'state' => 'M09020');
            return $result;
        }
        $DefaultAddress = DB::table('addresses')->where(['user_id' => $user_id, 'address_type' => 1, 'state' => 1])->first();
        if ($DefaultAddress != null && $isDefault == 1) {
            $address_id = $DefaultAddress->address_id;
            DB::table('addresses')->where(['address_id' => $address_id])
                ->update(['address_type' => 0, 'updated_at' => time()]);
        }
        DB::table('addresses')->insert([
            'user_id' => $user_id,
            'consignee' => $consignee,
            'phone' => $phone,
            'country' => $country,
            'city' => $city,
            'address' => $address,
            'address_type' => $isDefault,
            'created_at' => $time,
            'updated_at' => $time,
        ]);
        $info = DB::table('addresses')
            ->where('user_id', $user_id)
            ->orderBy('address_id', 'desc')
            ->select('address_id', 'consignee', 'phone', 'city', 'country', 'address', 'address_type')
            ->first();
        $result = array('message' => '添加成功', 'result' => $info, 'state' => 'M00000');
        return $result;
    }

    /**
     * 设置默认收货地址
     */
    public function setDefault(Request $request)
    {
        $user_id = $request->get('user_id');
        if (strlen($user_id) == 0) {
            $result = array('message' => '请先登录', 'result' => (Object) array(), 'state' => 'M09000');
            return $result;
        }
        $address_id = $request->get('address_id');
        $hasDefault = DB::table('addresses')->where(['user_id' => $user_id, 'address_type' => 1])->first();
        if ($hasDefault != null) {
            DB::table('addresses')->where(['user_id' => $user_id, 'address_type' => 1])->update(
                [
                    'address_type' => 0,
                    'updated_at' => time(),
                ]
            );
        }
        DB::table('addresses')->where('address_id', $address_id)->update(
            [
                'address_type' => 1,
                'updated_at' => time(),
            ]
        );
        $result = array('message' => '设置默认地址成功', 'result' => (Object) array(), 'state' => 'M00000');
        return $result;
    }

    /**
     * 修改收货地址
     */
    public function editAddress(Request $request)
    {
        $user_id = $request->get('user_id');
        $address_id = $request->get('address_id');
        $consignee = $request->get('consignee');
        if (mb_strlen($consignee) > 12) {
            $arr = array('message' => '收货人姓名太长!!!', 'result' => (object) array(), 'state' => 'M09009');
            return $arr;
        }
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $consignee) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i", json_encode($consignee)) != 0) {
            //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '收货人姓名中不能含有特殊字符', 'result' => (object) array(), 'state' => 'M09009');
            return $arr;
        }
        $phone = $request->get('phone');
        $country = $request->get('country', '');
        $city = $request->get('city');
        $address = $request->get('address');
        if (preg_match("/(\\\u[ed][0-9a-f]{3})/i", json_encode($address)) != 0) {
            //不允许特殊字符 判断emoji表情
            $arr = array('message' => '详细地址中不能含有表情', 'result' => (object) array(), 'state' => 'M09009');
            return $arr;
        }
        $isDefault = $request->get('is_default');

        if (strlen($consignee) == 0 || strlen($phone) == 0 || strlen($city) == 0 || strlen($address) == 0) {
            $result = array('message' => '请检查信息完整', 'result' => (Object) array(), 'state' => 'M00002');
            return $result;
        }
        $check = $this->UserTool->checkPhone($phone);
        if ($check == false) {
            $result = array('message' => '手机号码格式不正确', 'result' => (Object) array(), 'state' => 'M09020');
            return $result;
        }
        if ($isDefault == 1) {
            DB::table('addresses')
                ->where('user_id', $user_id)
                ->update(
                    ['address_type' => 0,
                        'updated_at' => time()]);
        }
        DB::table('addresses')->where('address_id', $address_id)->update([
            'address_id' => $address_id,
            'consignee' => $consignee,
            'phone' => $phone,
            'country' => $country,
            'city' => $city,
            'address_type' => $isDefault,
            'address' => $address,
            'updated_at' => time(),
        ]);
        $info = DB::table('addresses')->where('address_id', $address_id)
            ->select('address_id', 'consignee', 'phone', 'city', 'country', 'address', 'address_type')
            ->first();
        $result = array('message' => '编辑成功', 'result' => $info, 'state' => 'M00000');
        return $result;
    }

    /**
     * 删除收货地址
     */
    public function deleteAddress(Request $request)
    {
        $address_id = $request->get('address_id');
        $user_id = $request->get('user_id');
        DB::table('addresses')->where(['address_id' => $address_id, 'user_id' => $user_id])->delete();
        $result = array('message' => '删除成功', 'result' => (Object) array(), 'state' => 'M00000');
        return $result;
    }

    /**
     * 收货地址列表
     */
    public function addressList(Request $request)
    {
        $user_id = $request->get('user_id');
        if (strlen($user_id) == 0) {
            $result = array('message' => '请先登录', 'result' => (Object) array(), 'state' => 'M09000');
            return $result;
        }
        $addressList = DB::table('addresses')
            ->where(['user_id' => $user_id, 'state' => 1])
            ->select('address_id', 'consignee', 'phone', 'country', 'city', 'address', 'address_type')
            ->orderBy('address_type', 'desc')
            ->orderBy('created_at', 'desc')
            ->get();
        if (count($addressList) == 0) {
            $arr = array('message' => '请添加收货地址', 'result' => $addressList, 'state' => 'M00000');
            return $arr;
        } else {
            $arr = array('message' => '用户收货信息列表', 'result' => $addressList, 'state' => 'M00000');
            return $arr;
        }
    }

    /**
     * 根据address_id获取收货地址信息
     */
    public function addressByID(Request $request)
    {
        $address_id = $request->get('address_id');
        $info = DB::table('addresses')->where('address_id', $address_id)
            ->select('address_id', 'consignee', 'phone', 'city', 'country', 'address', 'address_type')
            ->first();
        if ($info == null) {
            $arr = array('message' => '用户收货信息', 'result' => (Object) array(), 'state' => 'M00000');
            return $arr;
        } else {
            $arr = array('message' => '用户收货信息', 'result' => $info, 'state' => 'M00000');
            return $arr;
        }
    }
}
